﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class MaterialView : AuthBasePage
    {//by zhang
        #region
        private DataTable CreateUpFileDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AttachmentID");
            dt.Columns.Add("FileType");
            dt.Columns.Add("FileSize");
            dt.Columns.Add("UpDateTime");
            dt.Columns.Add("FileURL");
            dt.Columns.Add("FileName");
            dt.Columns.Add("FormType");
            dt.Columns.Add("FormID");
            dt.Columns.Add("Status");
            return dt;
        }
        protected DataTable DtDUpFile
        {
            get
            {
                if (Session["SessionPdf"] != null)
                {
                    return (DataTable)Session["SessionPdf"];
                }
                else
                {
                    return CreateUpFileDT();
                }
            }
            set
            {
                Session["SessionPdf"] = value;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["SessionPdf"] = null; //by zhang
                
                BindDDLMatType();
                BindDDLMatSmallType();
                BindDDLMaterialStatus();
                BindData();

                ddlMatTypeID.Attributes.Add("Enabled", "false");
                ddlMatSmallTypeID.Attributes.Add("Enabled", "false");

                chkIsDirect.Attributes.Add("Enabled", "false");

            }
          
        }
        

        /// <summary>
        /// 绑定原始数据
        /// </summary>
        private void BindData()
        {
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                BindMaterial(id);
                BindMaterialdetial(id);
            }
        }
        /// <summary>
        /// 绑定原料资料
        /// </summary>
        /// <param name="id"></param>
        private void BindMaterial(string id)
        {
            MdMaterialEntity entity = MdMaterial.GetMdMaterialByID(id);
            if (entity != null)
            {
                this.lblMaterialID.Text = entity.MaterialID.Trim();
                this.lblMaterialName.Text = entity.MaterialName.Trim();
                this.lblMinOrderNum.Text = entity.MinOrderWeight.ToString().Trim();
                this.lblArrivalCycle.Text = entity.ArrivalPeriod.ToString().Trim();
                this.lblSafeStock.Text = entity.SafeStock.ToString().Trim();
                this.lblSpecifications.Text = entity.Specifications.Trim();
                this.lblUnit.Text = entity.Unit.Trim();
                lblLong.Text = entity.Long.ToString();
                lblHeight.Text = entity.Height.ToString();
                lblWidth.Text = entity.Width.ToString();

                lblOutSide.Text = entity.OutSide.ToString();
                lblInSide.Text = entity.InSide.ToString();
                lblMMNumber.Text = entity.MMNumber.ToString();
                lblMeterWeight.Text = entity.MeterWeight.ToString();


                //this.ddlMaterialStatus.SelectedValue = entity.Status.Trim();
                this.ddlMatTypeID.SelectedValue = entity.MaterialTypeID.Trim();
                this.ddlMatSmallTypeID.SelectedValue = entity.MaterialSmallTypeID.Trim();
                this.chkIsDirect.Checked = entity.IsDirect == "1";
                this.ddlMatTypeID.Enabled = false;
                //this.lblFactoryType.Text = entity.fa.ToString().Trim(); //by zhang
            }
        }
        /// <summary>
        /// 绑定原料资料详细
        /// </summary>
        /// <param name="id"></param>
        private void BindMaterialdetial(string id)
        {

            //by zhang
            DataTable dTable = DtDUpFile.Copy();
            DataTable dtAttachment = MdAttachment.GetMdAttachment(id, "Material");
            if (dtAttachment != null && dtAttachment.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAttachment.Rows)
                {
                    DataRow d = dTable.NewRow();
                    d["AttachmentID"] = CommonMethod.GetString(dr["AttachmentID"]);
                    d["FileType"] = CommonMethod.GetString(dr["FileType"]);
                    d["FileSize"] = CommonMethod.GetString(dr["FileSize"]);
                    d["UpDateTime"] = CommonMethod.GetString(dr["UpDateTime"]);
                    d["FileURL"] = CommonMethod.GetString(dr["FileURL"]);
                    d["FileName"] = CommonMethod.GetString(dr["FileName"]);
                    d["FormType"] = CommonMethod.GetString(dr["FormType"]);
                    d["FormID"] = CommonMethod.GetString(dr["FormID"]);
                    d["Status"] = CommonMethod.GetString(dr["Status"]);
                    d["AttachmentID"] = CommonMethod.GetString(dr["AttachmentID"]);
                    dTable.Rows.Add(d);
                }
            }
            DtDUpFile = dTable.Copy();
            BindListData();
        }
        /// <summary>
        /// 绑定原料类型
        /// </summary>
        private void BindDDLMatType()
        {
            DataTable dt = MdMaterialtype.GetMdMaterialtypeAll().Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.ddlMatTypeID.DataSource = dt;
                this.ddlMatTypeID.DataTextField = "MaterialTypeName";
                this.ddlMatTypeID.DataValueField = "MaterialTypeID";
                this.ddlMatTypeID.DataBind();
                UIHelper.InsertFirstItem(ddlMatTypeID);
            }
        }

        DataTable dtSmallType = null;
        /// <summary>
        /// 绑定原料小类
        /// </summary>
        private void BindDDLMatSmallType()
        {
            string strMatTypeId = ddlMatTypeID.SelectedValue.Trim();
            dtSmallType = MdMaterialsmalltype.SelectByTypeID(strMatTypeId);
            if (dtSmallType.Rows.Count > 0)
            {
                this.ddlMatSmallTypeID.DataSource = dtSmallType;
                this.ddlMatSmallTypeID.DataTextField = "MaterialSmallTypeName";
                this.ddlMatSmallTypeID.DataValueField = "MaterialSmallTypeID";
                this.ddlMatSmallTypeID.DataBind();
            }
        }
        private void BindDDLMaterialStatus()
        {
            //BindParameterForListControl("MaterialStatus", ddlMaterialStatus);
            //UIHelper.InsertFirstItem(ddlMaterialStatus);
        }

        //by zhang
        #region 附档
        protected void btnAddDFileHid_Click(object sender, EventArgs e)
        {
            try
            {
                BindListData();
            }
            catch (Exception ex) { UIHelper.Alert(this.Page, FomatString(ex.Message.ToString())); }
        }

        private void BindListData()
        {
            try
            {
                lstFile.DataSource = DtDUpFile;
                lstFile.DataTextField = "FileName";
                lstFile.DataValueField = "AttachmentID";
                lstFile.DataBind();
                UIHelper.RunScript(this.Page, "iframeAutoFit(); ");
            }
            catch { }
        }
        protected void btnDelFile_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(lstFile.SelectedValue))
                {
                    DataTable dtTemp = DtDUpFile.Copy();
                    string strAttachmentID = lstFile.SelectedValue.Trim();
                    DataRow[] drsSelect = dtTemp.Select("AttachmentID='" + strAttachmentID + "'");
                    if (drsSelect != null && drsSelect.Length > 0)
                    {
                        dtTemp.Rows.Remove(drsSelect[0]);
                        dtTemp.AcceptChanges();
                        DtDUpFile = dtTemp.Copy();
                        BindListData();
                    }
                }
                else { UIHelper.AlertMessage(this.Page, "请选择删除附件！"); }
            }
            catch (Exception ex) { UIHelper.Alert(this.Page, FomatString(ex.Message.ToString())); }
        }

        protected void btnRead_Click(object sender, EventArgs e)
        {
            if (lstFile.Items.Count > 0)
            {
                if (!string.IsNullOrEmpty(lstFile.SelectedValue.Trim()))
                {
                    DataTable dt = DtDUpFile.Copy();
                    DataRow[] drs = dt.Select("AttachmentID='" + lstFile.SelectedValue.Trim() + "'");
                    if (drs.Length > 0)
                    {
                        DataRow dr = drs[0];
                        string path = "/UpFile/";
                        path += dr["FormType"] + "/" + dr["FileURL"];
                        path = Request.Url.Scheme + "://" + Request.Url.Authority + path;
                        UIHelper.RunScript(this, "window.open('" + path + "')");
                    }
                }
                else
                {
                    UIHelper.AlertMessage(this.Page, "请先选择要查看的附件！");
                }
            }
            else
            {
                UIHelper.AlertMessage(this.Page, "请先上传附件！");
            }
        }
        #endregion 


    }
}